package com.pactera.integral.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.pactera.integral.common.jpa.criteria.Criteria;
import com.pactera.integral.common.jpa.criteria.Restrictions;
import com.pactera.integral.dao.DepartmentDao;
import com.pactera.integral.dao.UserDao;
import com.pactera.integral.entity.Department;
import com.pactera.integral.entity.User;
import com.pactera.integral.service.DepartmentService;

@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {

	@Autowired
	private DepartmentDao departmentDao;
	
	@Autowired
	private UserDao userDao;
	
	/**
	 *  添加或修改部门
	 * @param department
	 * @throws Exception
	 */
	@Override
	public void save(Department department) throws Exception{
		Boolean isMod = Boolean.TRUE;
		if("'".equals(department.getDepartmentId())) isMod = Boolean.FALSE;
		String tempCode = department.getDepartmentCode();
		if(department.getSuperiorDepartmentCode() != null && !"".equals(department.getSuperiorDepartmentCode())){
			tempCode = department.getSuperiorDepartmentCode() + department.getDepartmentCode();
		}
		if(isMod) {
			Department existingEntity = departmentDao.findByDepartmentId(department.getDepartmentId());
			if(existingEntity != null) {
				if(!existingEntity.getDepartmentCode().equals(tempCode)) {
					checkIfExistingDepartmentCode(tempCode);
					List<User> list = userDao.findByDepartmentCode(department.getDepartmentCode());
					if(list != null && list.size() > 0){
						throw new Exception("部门编码已引用不可修改!");
					}
				}
				existingEntity.setDepartmentCode(tempCode);
				existingEntity.setDescription(department.getDescription());
				existingEntity.setName(department.getName());
				departmentDao.save(existingEntity);
			} else throw new Exception("提交了非法的数据，不能处理!");
		} else {
			checkIfExistingDepartmentCode(tempCode);
			department.setDepartmentCode(tempCode);
			departmentDao.save(department);
		}
	}


	/**
	 * 部门列表
	 * @param param
	 * @param page
	 * @return Page<Department>
	 */
	@Override
	public Page<Department> pageDepartment(Map<String, Object> param, Pageable page) {
		Criteria<Department> criteria = new Criteria<Department>();
    	if(param != null){
    		criteria.add(Restrictions.like("name", param.get("name").toString()));
    	}
		Page<Department> p = departmentDao.findAll(criteria,page);
    	
		return p;
		
	}
	
	/**
	 * 部门下拉列表
	 * @return List<Map<String,String>>
	 */
	@Override
	public List<Map<String, String>> departmentList() {
		List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
		List<Department> list = departmentDao.findAll();
		for (Department department : list) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("departmentCode", department.getDepartmentCode());
			map.put("name", department.getName());
			listMap.add(map);
		}
		
		return listMap;
	}
	

	/**
	 * 查看部门明细
	 * @param departmentId
	 * @return Department
	 */
	@Override
	public Department getDepartment(String departmentId) {
		Department department = departmentDao.findByDepartmentId(departmentId);
		if(department.getDepartmentCode().length()>2){
			String code = department.getDepartmentCode();	
			String superiorDepartmentCode = code.substring(0, code.length()-2);
			String departmentCode = code.substring(code.length()-2, code.length());
			department.setSuperiorDepartmentCode(superiorDepartmentCode);
			department.setDepartmentCode(departmentCode);
		}
		
		return department;
	}

	@Override
	public Boolean deleteDepartment(String departmentId) {
		Department department = departmentDao.getOne(departmentId);
		departmentDao.delete(department);
		return Boolean.TRUE;
	}

	private void checkIfExistingDepartmentCode(String departmentCode) throws Exception {
		Department existingEntity = departmentDao.findByDepartmentCode(departmentCode);
		if(existingEntity != null && existingEntity.getDepartmentCode().equals(departmentCode)) {
			throw new Exception("部门编码已存在!");
		}
	}
}
